/* 
 * File: AndroidFileIO
 * Created: 13-JUN-2011
 * Author: Colin Gall-McDaid
 * 
 * Description: Android implementation of the FileIO framework.
 */
package com.gallsoft.games.framework.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.res.AssetManager;
import android.os.Environment;

import com.gallsoft.games.framework.FileIO;

public class AndroidFileIO implements FileIO {

    private AssetManager assets;
    private String externalStoragePath;

    /**
     * Constructor
     * 
     * @param assets Android assets (resources) manager
     */
    public AndroidFileIO(AssetManager assets) {
        this.assets = assets;
        this.externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
    }

    /**
     * Open given Android asset (resource)
     * 
     * @param fileName asset filename
     */
    public InputStream readAsset(String fileName) throws IOException {
        return assets.open(fileName);
    }

    /**
     * Open file for reading
     * 
     * @param fileName filename to read from
     */
    public InputStream readFile(String fileName) throws IOException {
        return new FileInputStream(externalStoragePath + fileName);
    }

    /**
     * Open file for writing
     * 
     * @param fileName filename to write to
     */
    public OutputStream writeFile(String fileName) throws IOException {
        return new FileOutputStream(externalStoragePath + fileName);
    }

}
